package com.hollycrm.unicom.sheet.business.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hollycrm.springframework.web.struts.DispatchActionSupport;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.BeanResource;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.business.BusinessSupportManager;
import com.hollycrm.unicom.sheet.code.CodeManager;
import com.hollycrm.unicom.sheet.lightweight.IDealSheet;
import com.hollycrm.unicom.sheet.lightweight.IMainSheet;

/**
 * SheetPrintAction
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class SheetPrintAction extends DispatchActionSupport {
    /**
     * execDefault
     *
     * @param mapping ActionMapping
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception
     * @return ActionForward
     */
    public ActionForward execDefault(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        String mainSheetFlowNO = request.getParameter("mainSheetFlowNO");
        String archive = request.getParameter("archive");
        BusinessSupportManager bsm = getBusinessSupportManager(archive, request);
        CodeManager cm = (CodeManager) getBean(BeanResource.CODE_MANAGER);
        AuthToken authToken = (AuthToken) request.getSession().getAttribute(AuthToken.BEAN_ID);
        //----------------------------------------------------------------------
        IMainSheet mainSheet = bsm.retrieveMainSheet(mainSheetFlowNO);
        String sheetFlowNO = mainSheet.getSheetFlowNO();
        String title = Constants.getValue("PRINT_TITLE");
        String printer = authToken.getUserCode();
        String currentTime = ((SimpleDateFormat) getBean(BeanResource.SDF_Y2S)).format(new Date());

        List subDealSheets = bsm.listDealSheets(sheetFlowNO,true);
        List answerRecords = bsm.listAnswerRecords(sheetFlowNO);
        List routineNotes = bsm.listSheetRoutineNote(sheetFlowNO);
        List appus = bsm.listSheetAppu(sheetFlowNO);
        //----------------------------------------------------------------------
        request.setAttribute("mainSheet", mainSheet);
        request.setAttribute("title", title);
        request.setAttribute("printer", printer);
        request.setAttribute("currentTime", currentTime);
        request.setAttribute("subDealSheets",subDealSheets);
        request.setAttribute("answerRecords",answerRecords);
        request.setAttribute("routineNotes",routineNotes);
        request.setAttribute("appus",appus);
        //----------------------------------------------------------------------
        return mapping.findForward("success");
    }

    public ActionForward execDealPrint(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        String mainSheetFlowNO = request.getParameter("mainSheetFlowNO");
        String sheetFlowNO = request.getParameter("dealSheetFlowNO");
        String archive = request.getParameter("archive");
        BusinessSupportManager bsm = getBusinessSupportManager(archive, request);
        CodeManager cm = (CodeManager) getBean(BeanResource.CODE_MANAGER);
        AuthToken authToken = (AuthToken) request.getSession().getAttribute(AuthToken.BEAN_ID);
        //----------------------------------------------------------------------
        IMainSheet mainSheet = bsm.retrieveMainSheet(mainSheetFlowNO);
        IDealSheet dealSheet = bsm.retrieveDealSheet(sheetFlowNO);
        String title = Constants.getValue("PRINT_TITLE");
        String printer = authToken.getUserCode();
        String currentTime = ((SimpleDateFormat) getBean(BeanResource.SDF_Y2S)).format(new Date());

        List subDealSheets = bsm.listDealSheets(sheetFlowNO,false);
        List answerRecords = bsm.listAnswerRecords(sheetFlowNO);
        List routineNotes = bsm.listSheetRoutineNote(sheetFlowNO);
        List appus = bsm.listSheetAppu(sheetFlowNO);
        //----------------------------------------------------------------------
        request.setAttribute("mainSheet", mainSheet);
        request.setAttribute("dealSheet",dealSheet);
        request.setAttribute("title", title);
        request.setAttribute("printer", printer);
        request.setAttribute("currentTime", currentTime);
        request.setAttribute("subDealSheets",subDealSheets);
        request.setAttribute("answerRecords",answerRecords);
        request.setAttribute("routineNotes",routineNotes);
        request.setAttribute("appus",appus);
        //----------------------------------------------------------------------
        return mapping.findForward("send");
    }


    /**
     * getBusinessSupportManager
     *
     * @param archive archive
     * @param request HttpServletRequest
     * @return BusinessSupportManager
     */
    private BusinessSupportManager getBusinessSupportManager(String archive,
        HttpServletRequest request) {
        if (Constants.TRUE.equals(archive))
            return (BusinessSupportManager) getBean(BeanResource.ARCHIVEBUSINESSSUPPORT_MANAGER);
        else
            return (BusinessSupportManager) getBean(BeanResource.BUSINESSSUPPORT_MANAGER);
    }

}
